package com.situ.userWeb.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.userWeb.Service.IUserService;
import com.situ.userWeb.Service.Impl.UserServiceImpl;
import com.situ.userWeb.Util.JSONResult;
import com.situ.userWeb.Util.JSONUtil;
import com.situ.userWeb.entity.Department;
import com.situ.userWeb.entity.Role;
import com.situ.userWeb.entity.User;
import com.situ.userWeb.entity.UserRoleDepartment;


@WebServlet("/users")
public class UsersServlet extends HttpServlet{
	private IUserService userService = new UserServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		String method = req.getParameter("method");
		if (method == null) {
			method = "selectAll";
		}
		switch(method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "selectId":
			selectId(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "delete":
			delete(req, resp);
			break;
		case "deleteAll":
			deleteAll(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "selectDepartmentRole":
			selectDepartmentRole(req, resp);
			break;
		default:
			break;
		}
	}

	private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
		String[] ids = req.getParameterValues("ids[]");
		boolean isSuccess = userService.deleteAll(ids);
		System.out.println(isSuccess);
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "删除失败");
		}
		JSONUtil.obj2Json(jsonResult, resp);
		
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String age = req.getParameter("age");
		String departmentId = req.getParameter("departmentId");
		User user = new User(Integer.parseInt(id), name, password, email, Integer.parseInt(age), Integer.parseInt(departmentId));
		String[] roleId = req.getParameterValues("roleId");
		boolean isSuccess = userService.update(user, id, roleId);
		
		
		
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "修改成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "修改失败");
		}
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void selectDepartmentRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ArrayList<Role> list1 = userService.selectRole();
		ArrayList<Department> list2 = userService.selectDepartment();
		
		req.setAttribute("roleList", list1);
		req.setAttribute("departmentList", list2);
		req.getRequestDispatcher("/user_insert.jsp").forward(req, resp);
	}


	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String age = req.getParameter("age");
		String departmentId = req.getParameter("departmentId");
		User user = new User(Integer.parseInt(id), name, password, email, Integer.parseInt(age), Integer.parseInt(departmentId));
		boolean isSuccessUser = userService.insertUser(user);
		System.out.println(isSuccessUser);
		
		String[] roleId = req.getParameterValues("roleId");
		int count = userService.insertUR(id, roleId);
		
		
//		resp.sendRedirect(req.getContextPath() + "/users?method=selectAll");
		JSONResult jsonResult = null;
		if (isSuccessUser) {
			jsonResult = JSONResult.success("添加成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "添加失败");
		}
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void selectId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userId = req.getParameter("id");
		User user = userService.selectId(Integer.parseInt(userId));
		
		ArrayList<Department> list2 = userService.selectDepartment();
		ArrayList<Role> list1 = userService.selectRole();
		List<Integer> selectRoleId = userService.selectRoleIdByUser(userId);
		
		for (Integer integer : selectRoleId) {
			for (Role role : list1) {
				if (integer == role.getId()) {
					role.setChecked(true);
				}
			}
		}
		
		
		req.setAttribute("user", user);
		req.setAttribute("roleList", list1);
		req.setAttribute("departmentList", list2);
		req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
		
	
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		boolean isSuccess = userService.delete(Integer.parseInt(id));
		System.out.println(isSuccess);
		JSONResult jsonResult = null;
		if (isSuccess) {
			jsonResult = new JSONResult(JSONResult.SUCCESS, "删除成功");
		} else {
			jsonResult = new JSONResult(JSONResult.ERROR, "删除失败");
		}
//		resp.sendRedirect(req.getContextPath() + "/users?method=selectAll");
		JSONUtil.obj2Json(jsonResult, resp);
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		List<UserRoleDepartment> list = userService.selectAll();
//		
//		req.setAttribute("list", list);
//		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
		String page = req.getParameter("page");
		String limit = req.getParameter("limit");
//		if (page == null || "".equals(page)) {
//			page = "1";
//		}
//		if (limit == null || "".equals(limit)) {
//			limit = "5";
//		}
		System.out.println(page);
		System.out.println(limit);
		JSONResult jsonResult = userService.selectLayuiByPage(Integer.parseInt(page), Integer.parseInt(limit));
		
		JSONUtil.obj2Json(jsonResult, resp);
	}

}
